define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'deerhome/shop/index' + location.search,
                    add_url: 'deerhome/shop/add',
                    edit_url: 'deerhome/shop/edit',
                    del_url: 'deerhome/shop/del',
                    multi_url: 'deerhome/shop/multi',
                    import_url: 'deerhome/shop/import',
                    table: 'deerhome_shop',
                }
            });

            var table = $("#table");
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "门店名称";};
            table.on('post-common-search.bs.table', function (event, table) {
                
                var form = $("form", table.$commonsearch);
                $("input[name='deerhome_user_id']", form).addClass("selectpage")
                .data("source", "deerhome/user/index")
                .attr("placeholder", "UID/昵称/手机号")
                .data("primaryKey", "id")
                .data("field", "utel")
                .data("search-field", "id,uname,utel")
                .data("pagination", "true")
                .data("page-size", "10")
                .data("format-item", "UID{id} / {uname} / {utel}")
                .data("order-by", "id");

                Form.events.selectpage(form);

                if(Config.dialog==1){
                    $(".pull-right").hide();
                    return;
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                uniqueId:'id',
                searchFormVisible: Config.dialog==1,
                fixedColumns: true,
                fixedNumber: 2,
                fixedRightNumber: 1,
                onLoadSuccess: function (data) {
                    $(".btn-editone").attr('data-shade','0.5').attr('data-shade-close','false');
                    if(Config.dialog==1){
                        $(".btn-editone").hide();
                        $(".btn-delone").hide();
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false,sortable:true},
                        {field: 'logo_images', title: __('门店Logo'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mdname', title: __('Mdname'), operate: 'LIKE'},
                        {field: 'type', title: __('类型'),  addClass:"selectpicker",  searchList: {'1':'自营','2':'入驻'},operate:"=",formatter:function(value, row, index){
                            return value=="1"?"自营":"入驻";
                        }},
                        {field: 'deerhome_user_id', operate:'=', title: '店长',formatter:function(value, row, index){
                            if(value==0) return "-";
                            let html='<div style="display:flex;">';
                                html+='<div style="width:50px;display: flex;align-items: center;justify-content: center;">';
                                    html+='<img src="'+row.user.face_image+'" style="width:40px;height:40px;border-radius:10px;margin-right:5px;">';
                                html+='</div>';
                                html+='<div style="display: flex;flex-direction: column;align-items: flex-start;justify-content: center;">';
                                    html+='<div>'+row.user.uname+'&nbsp;<span class="label label-default">UID '+row.deerhome_user_id+'</span></div>';
                                    html+='<div>'+row.user.utel+'</div>';
                                html+='</div>';
                            html+='</div>';
                            return html;
                        }},
                        {field: 'open_time', title: __('营业开始'), operate: false},
                        {field: 'close_time', title: __('营业结束'), operate: false},
                        {field: 'worker_count', title: "服务人员数", operate: false},
                        // {field: 'address', title: __('Address'), operate: Config.dialog==1?false:'LIKE'},
                        {field: 'address1', title: __('省份'), operate:false},
                        {field: 'address2', title: __('城市'), operate: false},
                        {field: 'address3', title: __('区域'), operate: false},
                        // {field: 'reg_time', title: __('入驻时间'), operate:Config.dialog==1?false:'RANGE', addclass:'datetimerange', autocomplete:false,sortable:true},

                        {field: 'operate', title: __('Operate'), operate:false,width:80,visible: Config.dialog==1,
                         events: {
                            'click .btn-chooseone': function (e, value, row, index) {
                                let obj={id: row.id,name:row.mdname};
                                Fast.api.close([obj]);
                            },
                        },
                        formatter:function(value, row, index){
                            return `<a href="javascript:;" class="btn btn-xs btn-danger btn-chooseone"><i class="fa fa-check"></i> 选择</a>`
                        }},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,visible: Config.dialog!=1}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(".btn-select").on("click",function () {
                var ids=Table.api.selectedids(table);
                if(ids.length==0){
                    Toastr.error("请选择");
                    return;
                }
                let arr=[];
                ids.forEach(function (v,i) {
                    let row=table.bootstrapTable('getRowByUniqueId', v);
                    arr.push({
                        id: row.id
                        ,name:row.mdname
                    });
                });
                Fast.api.close(arr);
            })
        },       
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
